#include("header.html")

<div class="container">
    <div class="box box-success">
        <div class="box-header">
            <h4 class="box-title"><span class="fa fa-pencil-square-o"></span>修改密码</h4>
        </div>
        <div class="box-body">
            <form method="post" class="form-horizontal">
                <input type="hidden" id="id" name="id" value="${user.id}">
                <div class="form-group">
                    <div  id="error" style="color: red;margin-left: 40%;margin-bottom: 2%;display: none;"></div>
                    <label for="new_pass" class="control-label col-lg-2 col-sm-2 col-xs-4">新密码</label>
                    <div class="col-lg-10 col-sm-10">
                        <input type="password" name="password" id="new_pass" class="form-control" required>
                    </div>
                </div>
                <div class="form-group">
                    <label for="two_pass" class="control-label col-lg-2 col-sm-2 col-xs-4">确定密码</label>
                    <div class="col-lg-10 col-sm-10">
                        <input type="password" id="two_pass" class="form-control" required>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-lg-12" style="margin-top: 5px;">
                        <button type="submit" class="btn btn-info btn-block">确认修改</button>
                    </div>
                </div>
            </form>
            #include("sign.html")
        </div>
    </div>
</div>

<script>
    $('form').submit(function (e) {
        e.preventDefault();
        var sid = $('#id').val().trim();
        var new_pass = $('#new_pass').val().trim();
        var two_pass = $('#two_pass').val().trim();
        if(checkPassword()){
            document.getElementById('error').style.display='none';
            $.post('/user/change', {
                id: sid,
                password: new_pass
            }, function (data) {
                if (data.success) {
                    alert('SUCCESS: 修改成功！');
                    location.href = '/user/login'
                } else {
                    alert('ERROR: '+data.msg);
                }
            });
        }

    });

    function checkPassword() {
        if($('#new_pass').val().length<6){
            document.getElementById('error').innerText="密码不能小于6位！";
            document.getElementById('error').style.display='block';
            return 0;
        }
        else if(($('#new_pass').val()!==$('#two_pass').val())||$('#new_pass').val()===''||
            $('#two_pass').val()===''){
            document.getElementById('error').innerText="两次密码输入不一致！";
            document.getElementById('error').style.display='block';
            return 0;
        }
        else {
            return 1;
        }
    }

</script>
</body>
</html>